﻿using System.Security.Cryptography;
using AutoMapper;
using MediatR;
using Mysqlx.Crud;
using MySqlX.XDevAPI.Common;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.Materials;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;

namespace Production_Set.API.MaterialManagement.Add
{
    public class MaterialCreateHand : IRequestHandler<MaterialCreateCommon, THE_WIND_RISES.Common.Dtos.Result>
    {
        IBaseRepository<ItemInfo> ItemInfoRepository;

        public MaterialCreateHand(IBaseRepository<ItemInfo> itemInfoRepository, IMapper mapper)
        {
            ItemInfoRepository = itemInfoRepository;
            this.mapper = mapper;
        }

        public IMapper mapper { get; set; }

        /// <summary>
        /// 物料信息新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<THE_WIND_RISES.Common.Dtos.Result> Handle(MaterialCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<ItemInfo>(request);
            string sql = "insert into ItemInfo values(@InventoryInfoId,@ItemServeId,@ItemCode,@ItemName,@ItemTypeId,@ItemUnitId,@SpecName,@SpecsUnitId,@WrapSpecs,@IsQuality,@InventoryLimit,@InventoryMinimum,@InventoryAccount,@SalesRevenueAccount,@SoldAccount,@ItemQRCode,@ItemInfoState,@ReviewStatus,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = ItemInfoRepository.Add(sql, new
            {
                AddData.InventoryInfoId,
                AddData.ItemServeId,
                AddData.ItemCode,
                AddData.ItemName,
                AddData.ItemTypeId,
                AddData.ItemUnitId,
                AddData.SpecName,
                request.SpecsUnitId,
                AddData.WrapSpecs,
                AddData.IsQuality,
                AddData.InventoryLimit,
                AddData.InventoryMinimum,
                AddData.InventoryAccount,
                AddData.SalesRevenueAccount,
                AddData.SoldAccount,
                AddData.ItemQRCode,
                AddData.ItemInfoState,
                AddData.ReviewStatus,

                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
